#include "eeprom.h"
#include "Config.h"
#include "NVIC.h"
#include "SWITCH.h"
#include "UART.h"
#include <string.h>
#include "DELAY.H"

void UART_config(void) {
    COMx_InitDefine		COMx_InitStructure;					//结构定义

    COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;	//模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
    COMx_InitStructure.UART_BRT_Use   = BRT_Timer1;			//选择波特率发生器, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
    COMx_InitStructure.UART_BaudRate  = 115200ul;			//波特率, 一般 110 ~ 115200
    COMx_InitStructure.UART_RxEnable  = ENABLE;				//接收允许,   ENABLE或DISABLE
    COMx_InitStructure.BaudRateDouble = DISABLE;			//波特率加倍, ENABLE或DISABLE
    UART_Configuration(UART1, &COMx_InitStructure);		//初始化串口1 UART1,UART2,UART3,UART4

  	NVIC_UART1_Init(ENABLE,Priority_1);		//中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
    UART1_SW(UART1_SW_P30_P31);		// 引脚选择, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
}

// 定义外部存储器长度
#define EEPROM_SIZE 1024

//eeprom缓冲区
u8 xdata eeprom_buffer[EEPROM_SIZE];

//写u16到eeprome
void eeprom_write_int(u16 addr, u16 value){
    EEPROM_write_n(addr, (u8*)&value, 2);
}

//写u32到eeprome
void eeprom_write_long(u16 addr, u32 value){    
    EEPROM_write_n(addr, (u8*)&value, 4);
}

//写float到eeprome
void eeprom_write_float(u16 addr, float value){
    u32 temp = *(u32*)&value;
    EEPROM_write_n(addr, (u8*)&temp, 4);
}

//int读取并比较 
void eeprom_read_int_compare(u16 addr, u16 value){
    u16 read_value;
    u8 dat[2];
    EEPROM_read_n(addr, dat, 2);
    read_value = *((u16*)dat);
    printf("read_value = %d\r\n", read_value);
    printf("value = %d\r\n", value);
    if (read_value == value)     //比较成功 输出提示信息        
    {
        printf("eeprom_read_int_compare success\r\n");
    }else
    {
        printf("eeprom_read_int_compare fail\r\n");
    }
}
    
//long读取并比较 
void eeprom_read_long_compare(u16 addr, u32 value){
    u32 read_value;
    u8 dat[4];
    EEPROM_read_n(addr, dat, 4);
    read_value = *((u32*)dat);
    printf("read_value = %d\r\n", read_value);
    printf("value = %d\r\n", value);
    if (read_value == value)     //比较成功 输出提示信息        
    {
        printf("eeprom_read_long_compare success\r\n");
    }else
    {
        printf("eeprom_read_long_compare fail\r\n");
    }
}

//float读取并比较    
void eeprom_read_float_compare(u16 addr, float value){
    u32 read_value;
    u8 dat[4];
    EEPROM_read_n(addr, dat, 4);
    read_value = *((u32*)dat);
    printf("read_value = %f\r\n", *(float*)&read_value);
    printf("value = %f\r\n", value);
    if (*(float*)&read_value == value)     //比较成功 输出提示信息        
    {
        printf("eeprom_read_float_compare success\r\n");
    }else
    {
        printf("eeprom_read_float_compare fail\r\n");
    }
}

void main(void)
{    
    u16 addr_int = 0x0000;  
    u16 int_value = 0x1234;

    u16 addr_long = 0x0004;
    u32 long_value = 0x12345678;

    u16 addr_float = 0x0008;
    float float_value = 3.1415926;

    u16 addr = 0x0012;
    char *str = "hello world";
    u16 len = strlen(str);

    eeprom_buffer[len] = '\0';

    UART_config();
    EA = 1;
    EEPROM_write_n(addr, (u8*)str, len);
    EEPROM_read_n(addr, eeprom_buffer, len);
    printf("str = %s\r\n", str);
    printf("eeprom_buffer = %s\r\n", eeprom_buffer);
    //比较字符串
    if (strcmp(eeprom_buffer, str) == 0)     //比较成功 输出提示信息        
    {
        printf("eeprom_read_string_compare success\r\n");
    }else
    {
        printf("eeprom_read_string_compare fail\r\n");
    }
    //擦除扇区
    EEPROM_SectorErase(addr);
    //写数据到eeprom，int,long,float
    eeprom_write_int(addr_int, int_value);
    eeprom_write_long(addr_long, long_value);
    eeprom_write_float(addr_float, float_value);
    //读数据并比较
    eeprom_read_int_compare(addr_int, int_value);
    eeprom_read_long_compare(addr_long, long_value);
    eeprom_read_float_compare(addr_float, float_value);

    while (1)
    {
        delay_ms(20);
    }
    
}